package com.sinosoft.weixin.service;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.sinosoft.weixin.utils.ArabicToChineseUtils;


public class testpdf {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Document pdfDocument = new com.itextpdf.text.Document(PageSize.A4,2.5f,2.5f,2.5f,2.5f);
		
		String templatePath="/Users/huzhengying/Desktop/";
		String outfilename="test.pdf";
		try{
			PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(templatePath+outfilename));
			pdfDocument.open();
			PdfReader reader = new PdfReader(templatePath + "/ElectronicPlanBook1.pdf");

			for (int i = 1; i <= reader.getNumberOfPages(); i++){
				if(i == 1){
					Font lishu = getFont(
							"/Users/huzhengying/Desktop/字体/SIMLI.TTF",
							BaseFont.IDENTITY_H, 30, Font.NORMAL, BaseColor.BLACK);
					PdfImportedPage page = writer.getImportedPage(reader, i);
					Image image = Image.getInstance(page);
					image.setAbsolutePosition(0,0);
					pdfDocument.add(image);

					Paragraph paragraph = new Paragraph();
					paragraph.setAlignment(Element.ALIGN_CENTER);
					paragraph.setIndentationLeft(35);
					paragraph.setIndentationRight(35);
					pdfDocument.add(new Phrase("\n\n\n\n\n\n\n\n"));
					Chunk chunk = new Chunk(
							"瞎折腾公司瞎折腾公司瞎折腾公司瞎折腾公司瞎折腾公司瞎折腾公司瞎折腾公司"+"\n员工福利保障计划",lishu);
					chunk.setLineHeight(25f);
					paragraph.add(chunk);
					pdfDocument.add(paragraph);
					paragraph.clear();

					Font fangSong = getFont(
							"/Users/huzhengying/Desktop/字体/SIMYOU.TTF",
							BaseFont.IDENTITY_H,16,Font.NORMAL,BaseColor.BLACK);
					paragraph = new Paragraph();
					paragraph.setAlignment(Element.ALIGN_CENTER);
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月");
					paragraph.add(
							new Phrase("\n\n\n\n\n\n\n\n\n\n\n国联人寿\n\n"+simpleDateFormat.format(new Date()),fangSong));

					pdfDocument.add(paragraph);
					paragraph.clear();
					pdfDocument.newPage();
				}else if(i == 2){
					PdfImportedPage page = writer.getImportedPage(reader, i);
					Image image = Image.getInstance(page);
					image.setAbsolutePosition(0,0);
					pdfDocument.add(image);
					Paragraph paragraph = new Paragraph();
					paragraph.setAlignment(Element.ALIGN_LEFT);

					Font fangSong = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,16,Font.NORMAL,BaseColor.BLACK);
					paragraph.add(
							new Phrase("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" +
									"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" +
									"            服务人员:开门红            服务电话:18888888888",fangSong));
					pdfDocument.add(paragraph);

					Paragraph paragraph1 = new Paragraph();
					paragraph1.setAlignment(Element.ALIGN_CENTER);
					paragraph1.add(
							new Phrase("\n\n\n地址:南京中山南路342号南京互联网金融中心4F",fangSong));
					pdfDocument.add(paragraph1);

					paragraph.clear();
					paragraph1.clear();
					pdfDocument.newPage();
				} else if(i==4){
					PdfImportedPage page = writer.getImportedPage(reader, i);
					Image image = Image.getInstance(page);
					image.setAbsolutePosition(0,0);
					pdfDocument.add(image);

					Font fangSong = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,14,Font.NORMAL,BaseColor.BLACK);
					Paragraph paragraph = new Paragraph();
					paragraph.setAlignment(Element.ALIGN_LEFT);
					paragraph.setIndentationLeft(40);
					paragraph.setIndentationRight(40);
					Chunk chunk = new Chunk(
							String.format("\n\n\n\n\n\n致尊敬的:%s（以下简称贵公司）","南京龙腾机械设备制造有限公司"),fangSong);
					chunk.setLineHeight(25f);
					paragraph.add(chunk);

					pdfDocument.add(paragraph);
					paragraph.clear();
					pdfDocument.newPage();
				} else if(i == 7){
					PdfImportedPage page = writer.getImportedPage(reader, i);
					Image image = Image.getInstance(page);
					image.setAbsolutePosition(0,0);
					pdfDocument.add(image);

					Font fangSong = getFont("/Users/huzhengying/Desktop/字体/simkai.ttf",BaseFont.IDENTITY_H,28,Font.BOLD,BaseColor.BLACK);
					Paragraph paragraph = new Paragraph();
					paragraph.setAlignment(Element.ALIGN_CENTER);
					paragraph.add(new Phrase("\n\n\n\n\n\n\n保险方案介绍",fangSong));
					pdfDocument.add(paragraph);
					paragraph.clear();

					for(int j=1;j<2;j++){
						//添加方案标题
						paragraph = new Paragraph();
						paragraph.setAlignment(Element.ALIGN_LEFT);
						fangSong = getFont("/Users/huzhengying/Desktop/字体/simkai.ttf",BaseFont.IDENTITY_H,20,Font.BOLD,BaseColor.BLACK);
						paragraph.add(
								new Phrase(
										String.format("\n\n\n    保险方案（%s）",ArabicToChineseUtils.foematInteger(j)),
										fangSong));
						pdfDocument.add(paragraph);
						paragraph.clear();

						//添加保障责任表格
						paragraph = new Paragraph();
						paragraph.setAlignment(Element.ALIGN_LEFT);
						fangSong = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,15,Font.BOLD,BaseColor.BLACK);
						paragraph.add(new Phrase("\n     1.保障责任\n\n", fangSong));
						pdfDocument.add(paragraph);
						paragraph.clear();

						PdfPTable table = new PdfPTable(4);
						//设置表格宽度
						table.setWidthPercentage(85);
						//设置表格位置
						table.setHorizontalAlignment(Element.ALIGN_CENTER);
						Font tableTileFont = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,12,Font.BOLD,BaseColor.WHITE);
						PdfPCell titleCell;
						Paragraph titleParagraph = new Paragraph("团体人身、医疗及意外伤害保障",tableTileFont);
						titleParagraph.setAlignment(Element.ALIGN_CENTER);
						titleCell = new PdfPCell();
						titleCell.setBackgroundColor(new BaseColor(207,0,13));
						titleCell.setColspan(4);
						titleCell.setFixedHeight(25f);
						titleCell.setPhrase(titleParagraph);
						titleCell.setHorizontalAlignment(1);
						table.addCell(titleCell);

						PdfPCell contentCell;
						fangSong = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,11,Font.BOLD,BaseColor.BLACK);
						contentCell = new PdfPCell(new Phrase("保险项目",fangSong));
						contentCell.setFixedHeight(25f);
						contentCell.setHorizontalAlignment(1);
						table.addCell(contentCell);
						contentCell = new PdfPCell(new Phrase("保险责任描述",fangSong));
						contentCell.setHorizontalAlignment(1);
						table.addCell(contentCell);
						contentCell = new PdfPCell(new Phrase("保险金额（单位：元）",fangSong));
						contentCell.setHorizontalAlignment(1);
						table.addCell(contentCell);
						contentCell = new PdfPCell(new Phrase("保费（单位：元）",fangSong));
						contentCell.setHorizontalAlignment(1);
						table.addCell(contentCell);

						fangSong = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,10,Font.NORMAL,BaseColor.BLACK);
						for(int a=0;a<4;a++){
							contentCell = new PdfPCell(new Phrase("人身意外保险",fangSong));
							contentCell.setFixedHeight(20f);
							contentCell.setHorizontalAlignment(1);
							table.addCell(contentCell);

							contentCell = new PdfPCell(new Phrase("意外身故",fangSong));
							contentCell.setHorizontalAlignment(1);
							table.addCell(contentCell);
							contentCell = new PdfPCell(new Phrase("50,000",fangSong));
							contentCell.setHorizontalAlignment(1);
							table.addCell(contentCell);
							contentCell = new PdfPCell(new Phrase("350",fangSong));
							contentCell.setHorizontalAlignment(1);
							table.addCell(contentCell);
						}
						pdfDocument.add(table);
						//添加特别约定
						paragraph = new Paragraph();
						fangSong = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,15,Font.BOLD,BaseColor.BLACK);
						paragraph.add(new Phrase("\n     2.特别约定\n", fangSong));
						pdfDocument.add(paragraph);
						paragraph.clear();
						paragraph = new Paragraph();
						paragraph.setFirstLineIndent(10);
						paragraph.setIndentationLeft(35);
						paragraph.setIndentationRight(35);
						fangSong = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,13,Font.NORMAL,BaseColor.BLACK);
						Chunk chunk = new Chunk("  1:意外伤害保险中的伤残责任：承担因工伤所致伤残，赔付标准按《劳动能力鉴定 职工工伤与职业病致残等级》（GB/T16180-2014）认定的残疾等级评级，理赔时需提供社会保险行政部门出具的工伤认定证明及社会保险行政部门认可的劳动能力鉴定机构出具的被保险人的工伤残疾程度鉴定书，无工伤保险的需单位出具情况说明及公司指定鉴定机构的鉴定报告；非工伤导致的伤残按照我司条款认定残疾标准。\n" +
								"   行业标准伤残具体给付比例为：一级100%，二级90%，三级80%，四级70%，五级60%，六级50%，七级40%，八级30%，九级20%，十级10%。\n" +
								"   工伤标准伤残具体给付比例为：一级100%、二级75%、三级50%、四级30%、五级20%、六级15%、七级10%、八级7%、九级5%、十级3%.\n" +
								"  2:附加意外伤害团体医疗保险：免赔额为元100/次，给付比例为90.0%\n" +
								"  3:附加意外住院补贴团体医疗保险：20元/天，意外住院补贴单次以90天为限，全年以180天\n" +
								"  4:根据《特种作业人员安全技术培训考核管理规定》（国家安全生产监督管理总局第30号令）被保险人从事高空作业时，必须按照相关行业安全规定（包括但不限于必须佩带安全绳、安全带或安全防护网架等安全设施设备）开展作业活动，对于违反上述规定的，本公司对可能发生的人身伤亡及医疗费用不承担赔偿责任。",fangSong);
						chunk.setLineHeight(21f);

						paragraph.add(chunk);
						pdfDocument.add(paragraph);
						paragraph.clear();

					}

					pdfDocument.newPage();
				}else if(i == 9){
					//添加条款责任内容
					PdfImportedPage page = writer.getImportedPage(reader,7);
					Image image = Image.getInstance(page);
					image.setAbsolutePosition(0,0);
					pdfDocument.add(image);
					for(int j=1;j<2;j++){
						Font fangSong = getFont("/Users/huzhengying/Desktop/字体/simkai.ttf",BaseFont.IDENTITY_H,22,Font.BOLD,BaseColor.BLACK);
						Paragraph paragraph = new Paragraph();
						paragraph.setAlignment(Element.ALIGN_LEFT);
						paragraph.add(new Phrase("\n\n\n\n\n\n    条款责任内容\n",fangSong));
						pdfDocument.add(paragraph);
						paragraph.clear();;

						paragraph = new Paragraph();
						paragraph.setFirstLineIndent(16);
						paragraph.setIndentationLeft(32);
						paragraph.setIndentationRight(32);
						fangSong = getFont("/Users/huzhengying/Desktop/字体/SIMYOU.TTF",BaseFont.IDENTITY_H,14,Font.NORMAL,BaseColor.BLACK);
						Chunk chunk = new Chunk(j+".  "+"这是一个测试",fangSong);
						chunk.setLineHeight(25f);
						paragraph.add(chunk);
						pdfDocument.add(paragraph);
						paragraph.clear();;
					}

					pdfDocument.newPage();
				} else {
					PdfImportedPage page = writer.getImportedPage(reader, i);
					Image image = Image.getInstance(page);
					image.setAbsolutePosition(0,0);
					pdfDocument.add(image);
					pdfDocument.newPage();
				}

			}

		}catch (Exception e) {
			pdfDocument.close(); 
			e.printStackTrace();
		} finally {
			pdfDocument.close();
		}
	}

	private static Font getFont(String fontTtfUrl,String encoding,int fontSize,int fontWide,BaseColor baseColor) throws Exception{
		BaseFont baseFont = BaseFont.createFont(
				fontTtfUrl,
				encoding,
				BaseFont.NOT_EMBEDDED);
		Font font = new Font(baseFont,fontSize,fontWide,baseColor);
		return font;
	}

}
